package com.dankegongyu.lib.common.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;


/**
 * Created by Admin on 2018/4/9.
 */
@SuppressWarnings("JavaDoc")
public class ShapeFactory {

    private ShapeFactory() {
        throw new AssertionError("cannot be instantiated");
    }

    /**
     * 圆形shape
     *
     * @param context
     * @param solidColor 背景色
     * @return
     */
    public static Drawable createCircleShape(Context context, @ColorInt int solidColor) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.OVAL);
        shape.setColor(solidColor);
        return shape;
    }

    /**
     * 圆形shape
     *
     * @param context
     * @param solidColor    背景色
     * @param strokeColor   边框色
     * @param strokeWidthDP 边框宽度
     * @return
     */
    public static Drawable createCircleShape(Context context, @ColorInt int solidColor, @ColorInt int strokeColor, float strokeWidthDP) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.OVAL);
        shape.setColor(solidColor);
        shape.setStroke(ScreenUtil.dp2px(strokeWidthDP), strokeColor);
        return shape;
    }

    /**
     * 圆角shape
     *
     * @param context
     * @param radiusDP   圆角半径
     * @param solidColor 背景色
     * @return
     */
    public static Drawable createRectShape(Context context, float radiusDP, @ColorInt int solidColor) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setCornerRadius(ScreenUtil.dp2px(radiusDP));
        shape.setColor(solidColor);
        return shape;
    }

    /**
     * 圆角shape
     *
     * @param context
     * @param radiusDP      圆角半径
     * @param solidColor    背景色
     * @param strokeColor   边框色
     * @param strokeWidthDP 边框宽度
     * @return
     */
    public static Drawable createRectShape(Context context, float radiusDP, @ColorInt int solidColor, @ColorInt int strokeColor, float strokeWidthDP) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setCornerRadius(ScreenUtil.dp2px(radiusDP));
        shape.setColor(solidColor);
        shape.setStroke(ScreenUtil.dp2px(strokeWidthDP), strokeColor);
        return shape;
    }

    /**
     * 圆角渐变
     *
     * @param context
     * @param radiusDP    圆角半径
     * @param orientation 渐变方向
     * @param colors      渐变色，{startColor, centerColor, endColor} centerColor可不传
     * @return
     */
    public static Drawable createGradientShape(Context context, float radiusDP, GradientDrawable.Orientation orientation, @ColorInt int[] colors) {
        GradientDrawable shape = new GradientDrawable(orientation, colors);
        shape.setCornerRadius(ScreenUtil.dp2px(radiusDP));
        return shape;
    }

    /**
     * 圆形shape
     * Deprecated!
     * use {@link #createCircleShape(Context, int)} or {@link #createCircleShape(Context, int, int, float)} instead
     *
     * @param context
     * @param color   背景色
     * @return
     */
    @Deprecated
    public static Drawable getCircleShape(Context context, @ColorInt int color) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.OVAL);
        shape.setColor(color);
        return shape;
    }

    /**
     * 圆角shape
     * Deprecated!
     * use {@link #createRectShape(Context, float, int)} or {@link #createCircleShape(Context, int, int, float)} instead
     *
     * @param context
     * @param radiusDP 圆角半径
     * @param color    背景色
     * @return
     */
    @Deprecated
    public static Drawable getCornerShape(Context context, float radiusDP, @ColorInt int color) {
        GradientDrawable shape = new GradientDrawable();
        radiusDP = ScreenUtil.dp2px(radiusDP);
        shape.setCornerRadius(radiusDP);
        shape.setColor(color);
        return shape;
    }

    /**
     * 有圆角的渐变shape
     *
     * @param radiusDP 圆角半径
     * @param angle    渐变角度
     * @param colors   渐变色
     * @return
     */
    public static Drawable getGradientShape(Context context, float radiusDP, int angle, @ColorInt int[] colors) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setGradientRadius(angle);
        radiusDP = ScreenUtil.dp2px(radiusDP);
        shape.setCornerRadius(radiusDP);
        shape.setColors(colors);
        return shape;
    }

    /**
     * 可以分别设置四个角的Corner
     *
     * @param radiusArray 长度为8: 1、2两个参数表示左上角，3、4表示右上角，5、6表示右下角，7、8表示左下角
     * @param color       背景色
     * @return
     */
    public static Drawable get4SideCornerShape(Context context, float[] radiusArray, @ColorInt int color) {
        if (null == radiusArray || radiusArray.length != 8) {
            radiusArray = null;
        } else {
            for (int i = 0, length = radiusArray.length; i < length; i++) {
                radiusArray[i] = ScreenUtil.dp2px(radiusArray[i]);
            }
        }
        GradientDrawable shape = new GradientDrawable();
        shape.setCornerRadii(radiusArray);
        shape.setColor(color);
        return shape;
    }

    /**
     * 带边框、有圆角、无背景色的渐变shape
     *
     * @param strokeWidthDP 边框宽度
     * @param strokeColor   边框颜色
     * @param radiusDP      圆角半径
     * @param angle         角度
     * @return
     */
    public static Drawable getRectStrokeCornerShape(Context context, int strokeWidthDP, @ColorInt int strokeColor, float radiusDP, int angle) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.RECTANGLE);
        shape.setStroke(ScreenUtil.dp2px(strokeWidthDP), strokeColor);
        shape.setGradientRadius(angle);
        shape.setCornerRadius(ScreenUtil.dp2px(radiusDP));
        return shape;
    }

    /**
     * 带边框、无背景色的渐变shape
     * 注意：在自定义cell中使用 Activity进入后台在进入前台 底部边框可能会消失 使用xml配置不会出现该问题
     *
     * @param strokeWidthDP 边框宽度
     * @param strokeColor   边框颜色
     * @return
     */
    public static Drawable getCircleStrokeShape(Context context, int strokeWidthDP, @ColorInt int strokeColor) {
        GradientDrawable shape = new GradientDrawable();
        shape.setShape(GradientDrawable.OVAL);
        shape.setStroke(ScreenUtil.dp2px(strokeWidthDP), strokeColor);
        return shape;
    }

    /**
     * 方形颜色背景状态选择器shape
     *
     * @param normal
     * @param pressed
     * @param focused
     * @param disable
     * @return
     */
    public static Drawable getRectColorSelectorShape(Context context, @ColorInt int normal, @ColorInt int pressed, @ColorInt int focused, @ColorInt int disable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            GradientDrawable shape = new GradientDrawable();
            ColorStateList colorStateList = getColorSelectorStateList(context, normal, pressed, focused, disable);
            shape.setShape(GradientDrawable.RECTANGLE);
            shape.setColor(colorStateList);
            return shape;
        }
        return getDrawableStateListByColor(context, normal, pressed, focused, disable);
    }

    /**
     * 通过颜色int创建状态选择器
     * https://blog.csdn.net/CrazyMo_/article/details/53456541
     */
    public static ColorStateList getColorSelectorStateList(Context context, @ColorInt int normal, @ColorInt int pressed, @ColorInt int focused, @ColorInt int disable) {
        int[] colors = new int[]{pressed, focused, normal, focused, disable, normal};
        int[][] states = new int[6][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_enabled};
        states[3] = new int[]{android.R.attr.state_focused};
        states[4] = new int[]{android.R.attr.state_window_focused};
        states[5] = new int[]{};
        return new ColorStateList(states, colors);
    }

    /**
     * 通过drawable 的ID创建状态选择器
     * https://blog.csdn.net/CrazyMo_/article/details/53456541
     */
    public static StateListDrawable getDrawableStateList(Context context, @DrawableRes int idNormal, @DrawableRes int idPressed,
                                                         @DrawableRes int idFocused, @DrawableRes int idDisable) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
        Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
        Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
        Drawable disable = idDisable == -1 ? null : context.getResources().getDrawable(idDisable);
        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
        // View.ENABLED_FOCUSED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focused);
        // View.ENABLED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_enabled}, normal);
        // View.FOCUSED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_focused}, focused);
        // View.WINDOW_FOCUSED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_window_focused}, disable);
        // View.EMPTY_STATE_SET
        bg.addState(new int[]{}, normal);
        return bg;
    }

    /**
     * 通过drawable 的ID创建状态选择器
     * https://blog.csdn.net/CrazyMo_/article/details/53456541
     */
    public static StateListDrawable getDrawableStateListByColor(Context context, @ColorInt int normalColor, @ColorInt int pressedColor,
                                                                @ColorInt int focusedColor, @ColorInt int disableColor) {
        StateListDrawable bg = new StateListDrawable();
        GradientDrawable normalColorDrawable = new GradientDrawable();
        normalColorDrawable.setColor(normalColor);
        GradientDrawable pressedColorDrawable = new GradientDrawable();
        pressedColorDrawable.setColor(pressedColor);
        GradientDrawable focusedColorDrawable = new GradientDrawable();
        focusedColorDrawable.setColor(focusedColor);
        GradientDrawable disableColorDrawable = new GradientDrawable();
        disableColorDrawable.setColor(disableColor);
        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressedColorDrawable);
        // View.ENABLED_FOCUSED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focusedColorDrawable);
        // View.ENABLED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_enabled}, normalColorDrawable);
        // View.FOCUSED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_focused}, focusedColorDrawable);
        // View.WINDOW_FOCUSED_STATE_SET
        bg.addState(new int[]{android.R.attr.state_window_focused}, disableColorDrawable);
        // View.EMPTY_STATE_SET
        bg.addState(new int[]{}, normalColorDrawable);
        return bg;
    }

}
